/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.logging;

import org.testis.utils.StackTrace;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;

public class LogSenderService {

    private JmsTemplate m_jmsTemplateQueue;
    private static LogSenderService m_uniqueInstance = null;

    private LogSenderService() {
    }

    public synchronized static LogSenderService createInstance(
            JmsTemplate jmsTemplateQueue) {
        if (m_uniqueInstance == null) {

            m_uniqueInstance = new LogSenderService();
            m_uniqueInstance.m_jmsTemplateQueue = jmsTemplateQueue;
        }

        return m_uniqueInstance;
    }

    public synchronized static void putHeaderMessageOnQueue(int designTreeId,
            int designPartType,
            Byte result,
            int parentId,
            String name,
            String guid) {
        LogHeaderData data = new LogHeaderData(designTreeId,
                designPartType,
                result,
                parentId,
                name,
                guid);
        if (m_uniqueInstance != null) {
            m_uniqueInstance.send(data);
        } else {
            System.out.println(designTreeId + " --> " + designPartType + " --> "
                    + result + " --> " + designPartType + " --> "
                    + name + " --> " + guid);
        }
    }

    public synchronized static void putHeaderMessageOnQueue(int designTreeId,
            int designPartType,
            Byte result,
            int parentId,
            String name,
            String guid,
            String executionGuid) {
        LogHeaderData data = new LogHeaderData(designTreeId,
                designPartType,
                result,
                parentId,
                name,
                guid,
                executionGuid);
        if (m_uniqueInstance != null) {
            m_uniqueInstance.send(data);
        } else {
            System.out.println(designTreeId + " --> " + designPartType + " --> "
                    + result + " --> " + designPartType + " --> "
                    + name + " --> " + guid);
        }
    }

    public synchronized static void putHeaderMessageOnQueue(int designTreeId,
            int designPartType,
            Byte result,
            String parentGuid,
            String name,
            String guid,
            String executionGuid) {
        LogHeaderData data = new LogHeaderData(designTreeId,
                designPartType,
                result,
                parentGuid,
                name,
                guid,
                executionGuid);
        if (m_uniqueInstance != null) {
            m_uniqueInstance.send(data);
        } else {
            System.out.println(designTreeId + " --> " + designPartType + " --> "
                    + result + " --> " + designPartType + " --> "
                    + name + " --> " + guid);
        }
    }

    public synchronized static void putMessageOnQueue(String objectDesc,
            AtofLogLevel level,
            String logMsg,
            String guid) {
        if (guid == null || guid.isEmpty()) {
            return;
        }
        String className = StackTrace.getExecutingClassNameByIndex(2);

        LogDetailData data = new LogDetailData(className,
                objectDesc,
                level,
                logMsg,
                guid);
        if (m_uniqueInstance != null) {
            m_uniqueInstance.send(data);
        } else {
            System.out.println(objectDesc + " --> " + logMsg);
        }
    }

    public synchronized static void putFileMessageOnQueue(String objectDesc,
            AtofLogLevel level,
            String logMsg,
            String guid) {
        String className = StackTrace.getExecutingClassNameByIndex(2);

        FileLogData data = new FileLogData(className,
                objectDesc,
                level,
                logMsg,
                guid);
        if (m_uniqueInstance != null) {
            m_uniqueInstance.send(data);
        } else {
            System.out.println(objectDesc + " --> " + logMsg);
        }
    }

    private synchronized void send(final LogData data) {
        data.printMessage();
        for (int i = 0; i < 3; i++) {
            try {
                m_jmsTemplateQueue.convertAndSend(data);
                break;
            } catch (JmsException ex) {
                System.out.println("Retry index For Queue:" + i + "Could not send log data..." + ex.getMessage());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex1) {
                }
            } catch (Exception ex) {
                System.out.println("Exception occured...Could not send log data..." + ex.getMessage());
            }
        }
    }
}
